<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    
    <title>院系管理</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link rel="stylesheet"
		href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/dwr/interface/studentAction.js'></script>
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/dwr/interface/dWRCollegeMajorAction.js'></script>
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/dwr/engine.js'></script>
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/dwr/util.js'></script>
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/js/jqueryCopyEvent.js"></script>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/js/jquery.corner.js"></script>
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
	<script type="text/javascript">
		//=============初始化开始==========
		$j(function(){
			showAllCollege($('seleCollege'));
			showAllMajor($('seleMajor'));
		})
		//-------------初始化结束----------
		//============自定义函数开始=======
		//显示所有的学院
		function showAllCollege(collegeObj){
			DWREngine.setAsync(false);  //dwr同步
			dWRCollegeMajorAction.getAllCollegeInfo(function (collegeList){
					if(null == collegeList){
						return;
					}
					var collegeInfo =collegeList;
					var s =collegeObj;
					 	DWRUtil.removeAllOptions(s);  
						s.options[0] = new Option("不限学院",'0');
						//alert(collegeList[0].collegeinfoname)
					for ( var i = 1; i < collegeList.length+1; i++) {
						var opt = new Option(collegeList[i-1].collegeinfoname,
								collegeList[i-1].collegeinfoid);
						s.options[i] = opt;
					}
			});
			DWREngine.setAsync(true);  //恢复dwr异步
		
		}
		//显示所有的专业,obj为显示下拉列表元素
		function showAllMajor(obj){
			DWREngine.setAsync(false);  //dwr同步
			dWRCollegeMajorAction. getAllMajorInfo(function (majorList){
					if(null == majorList){
						return;
					}
					var majorInfo =majorList;
					//alert(aptValue[0].apartmentfloornum);
					var s = obj;
					 	DWRUtil.removeAllOptions(s);  
						s.options[0] = new Option("不限专业",'0');
						//alert(majorList[0].majorinfoname)
					for ( var i = 1; i < majorList.length+1; i++) {
						var opt = new Option(majorList[i-1].majorinfoname,
								majorList[i-1].majorinfoid);
						s.options[i] = opt;
					}
			});
			DWREngine.setAsync(true);  //恢复dwr异步
		}
		//根据学院显示所有的专业
		function showMajorByCollegeinfoid(obj,collegeinfoid){
			dWRCollegeMajorAction.getMajorInfoByCollegeId(collegeinfoid, function  (majorList){
				var s =$j("#seleMajor").get(0);
				if(majorList == null){
					DWRUtil.removeAllOptions(s); 
					s.options[0] = new
					 Option("不存在专业",'0');
					return;
				}
				var majorInfo =majorList;
				//alert(aptValue[0].apartmentfloornum);
				 	DWRUtil.removeAllOptions(s);  
					s.options[0] = new Option("不限专业",'0');
					//alert(majorList[0].majorinfoname)
				for ( var i = 1; i < majorList.length+1; i++) {
					var opt = new Option(majorList[i-1].majorinfoname,
							majorList[i-1].majorinfoid);
					s.options[i] = opt;
				}
			});
			
		}
		//选择的专业变动的时候，学院联动
		function selectedMajorChange(obj){
			major = obj;
			if(obj.options[obj.selectedIndex].value == '0'){
				return;
			}
			showCollegeByMajorInfoid(obj,obj.options[obj.selectedIndex].value);
		}
		//根据专业显示学院
		function showCollegeByMajorInfoid(obj,majorinfoid){
			dWRCollegeMajorAction.getCollegeInfoByMajorId(majorinfoid,function (collegeList){
				var college = $j("#seleCollege").get(0);
				for(var a=0;a<college.options.length;a++){
					if(college.options[a].value == collegeList[0].collegeinfoid){
						college.options[a].selected = "selected";
						$j("#collegeid_update").get(0).value = college.options[a].value;
						$j("#collegename_update").get(0).value = college.options[a].text;
			
						break;
					}
				}
			});
		}
		//选择的学院变化时
		function collegeChange(){
			if($j("#seleCollege").get(0).selectedIndex != 0){
				$j("#collegeid_update").get(0).value = $j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value;
				$j("#collegename_update").get(0).value = $j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].text;
				showMajorByCollegeinfoid($('seleMajor'),$j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value);
			}else{
				$j("#collegeid_update").get(0).value = "";
				$j("#collegename_update").get(0).value = "";
				showAllMajor($('seleMajor'));
			}

		}
		//选择的专业变化时
		function majorChange(){
			if($j("#seleMajor").get(0).selectedIndex != 0){
				$j("#majorid_update").get(0).value = $j("#seleMajor").get(0).options[$j("#seleMajor").get(0).selectedIndex].value;
				$j("#majorname_update").get(0).value = $j("#seleMajor").get(0).options[$j("#seleMajor").get(0).selectedIndex].text;
				selectedMajorChange($('seleMajor'));
			}
			else {
				$j("#majorid_update").get(0).value = "";
				$j("#majorname_update").get(0).value = "";
			}

	
		}
		//增加学院
		function addCollege(){
			dWRCollegeMajorAction.insertCollege($j("#addCollegeName").get(0).value,function (result){
				if(result == true){
					alert("操作成功!");
				}else{
					alert("添加学院失败");
				}
				showAllCollege($('seleCollege'));
			})
		}
		//删除学院
		function deleteCollege(){
			dWRCollegeMajorAction.deleteCollege($j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value,function (result){
				if(result == true){
					alert("操作成功!");
				}else{
					alert("删除学院失败");
				}
				showAllCollege($('seleCollege'));
			});
		}
		//修改学院
		function updateCollege(){
			dWRCollegeMajorAction.updateCollege($j("#collegeid_update").get(0).value,$j("#collegename_update").get(0).value,function (result){
				if(result > 0){
					alert("操作成功");
				}else{
					alert("学院未修改");
				}
				showAllCollege($('seleCollege'));
			});
		}
		//增加专业
		function addMajor(){
			dWRCollegeMajorAction.insertMajor($j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value,$j("#addMajorName").get(0).value,function (result){
				if(result == true){
					alert("操作成功");
				}else{
					alert("增加专业失败");
				}
				showMajorByCollegeinfoid($('seleMajor'),$j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value);
			})
		}
		//删除专业
		function deleteMajor(){
			if($j("#seleMajor").get(0).selectedIndex== 0){
				return;
			}
			dWRCollegeMajorAction.deleteMajor($j("#seleMajor").get(0).options[$j("#seleMajor").get(0).selectedIndex].value,function (result){
				if(result == true){
					alert("删除成功");
				}else{
					alert("删除失败");
				}
				showMajorByCollegeinfoid($('seleMajor'),$j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value);
			});
		}
		//更新专业
		function updateMajor(){
			dWRCollegeMajorAction.updateMajor($j("#majorid_update").get(0).value,$j("#majorname_update").get(0).value,function (result){
				if(result > 0 ){
					alert("操作成功");
				}else{
					alert("专业未修改");
				}
				showMajorByCollegeinfoid($('seleMajor'),$j("#seleCollege").get(0).options[$j("#seleCollege").get(0).selectedIndex].value);
			});
		}
		//--------------自定义函数结束-----------
	</script>

  </head>
  
  <body>

		<jsp:include page="studentnav.jsp"></jsp:include>
		<h2>院系管理</h2>
		
		<div>
			
			<div class="divCreateCollege">
				<input type="text"　 id="addCollegeName" name="collegename" value="学院名" />
				<input type="button" id="btnCreateCollege" onclick="addCollege()" value="确认增加"/>
			</div>
			<div>
				<div >
					<div>
						<select  id="seleCollege" style="width:200" onchange="collegeChange()">
						</select>
						<input type="button" id="deleteCollege" onclick="deleteCollege()" value="删除学院"/>
					</div>
					<div style="margin-left:0px;">
						<label>学院代码</label><input type="text" id="collegeid_update" value="" readOnly= "true" /><br/>
						<label>学院名称</label><input type="text" id="collegename_update" value="" />
						<input type="button" onclick="updateCollege()" value="确认修改学院">
					</div>
				</div>
				<br/>
				<div style="margin-left:160px">
					<div>
						<input type="text" id="addMajorName"  value="专业名" />
						<input type="button" id="btnCreateMajor" onclick="addMajor()" value="确认增加"/>
					</div>
					<div >
						<select id="seleMajor" style="width:200" onchange="majorChange()">
						</select>
						<input type="button" id="deleteMajor" onclick="deleteMajor()" value="删除专业"/>
					</div>
					<div  style="margin-left:0px" >
						<label>专业代码</label><input type="text" id="majorid_update"  readOnly= "true"  /><br/>
						<label>专业名称</label><input type="text" id="majorname_update"  />
						<input type="button" onclick="updateMajor()" value="确认修改专业">
					</div>
				</div>
			</div>
		</div>
	 


  </body>
</html>
